﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CWB.Common.DTO
{
    public class Paging
    {
        private int _page;
        private int _totalRecord;

        public int pageSize;
        public int maxPage;

        public int? currentPage
        {
            get { return _page; }
            set
            {
                if ((value == null) || (value < 1))
                {
                    _page = 1;
                }
                else
                {
                    _page = (int)value;
                }
            }
        }
        public int totalRecord
        {
            get { return _totalRecord; }
            set
            {
                _totalRecord = value;
                if (_totalRecord == 0)
                {
                    maxPage = 0;
                }
                else
                {
                    maxPage = _totalRecord / pageSize;
                }
                if (pageSize * maxPage < _totalRecord) maxPage++;
                if (currentPage > maxPage)
                {
                    currentPage = maxPage;
                }
            }
        }
        public string getLimitSql()
        {
            if (pageSize == 0)
            {
                pageSize = 10;
            }
            string limit = string.Format(" limit {0}, {1}", pageSize * (_page - 1), pageSize);
            return limit;
        }
    }
}
